package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanBackQO;
import com.xingkeduo.pojo.table.CraftsmanBackTO;
import com.xingkeduo.pojo.table.CraftsmanTO;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * 求职者封装类
 * 
 * @author 高杰
 * @time 2018年12月25日 下午1:41:24
 * @description
 */
@Component
public class CraftsmanBackService {
	private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanBackTO.class);
	@Resource(name = "mysqlUserMasterDao")
	private DatabaseAccessObject<CraftsmanBackTO, CraftsmanBackQO> dao;
	private final static String errorMsg = "一线员工召回信息";

	/**
	 * 查询最新一条
	 * 
	 * @author 高杰
	 * @time 2019年3月14日 下午1:53:08
	 * @param craftsmanId
	 * @return
	 * @throws Throwable
	 */
	public CraftsmanBackTO selectLastByCraftsmanId(long craftsmanId) throws Throwable {
		CraftsmanBackQO query = new CraftsmanBackQO().setCraftsmanId(craftsmanId).setRecordStart(0)
				.setRecordEnd(1);
		List<CraftsmanBackTO> backList = selectByQuery(query);
		if (CommonUtil.isEmpty(backList))
			return null;
		return backList.get(0);
	}

	/**
	 * 查询
	 * 
	 * @author 高杰
	 * @time 2019年3月14日 下午1:53:03
	 * @param query
	 * @return
	 * @throws Throwable
	 */
	public List<CraftsmanBackTO> selectByQuery(CraftsmanBackQO query) throws Throwable {
		return dao.select(NAMESPACE, query);
	}

	/**
	 * 新增
	 * 
	 * @author 高杰
	 * @time 2019年3月14日 下午1:52:58
	 * @param craftsmanJobBack
	 * @throws Throwable
	 */
	@Transactional
	public void insert(CraftsmanBackTO craftsmanJobBack) throws Throwable {
		if (dao.insert(NAMESPACE, craftsmanJobBack) < 1)
			throw new DataException(DataException.Type.FAIL, errorMsg);
	}

	/**
	 * 新增
	 * 
	 * @author 高杰
	 * @time 2019年3月14日 下午1:52:53
	 * @param craftsmanJobBackList
	 * @param craftsman
	 * @param operator
	 * @throws Throwable
	 */
	@Transactional
	public void insert(List<CraftsmanBackTO> craftsmanJobBackList, CraftsmanTO craftsman, long operator)
			throws Throwable {
		for (Iterator<CraftsmanBackTO> iterator = craftsmanJobBackList.iterator(); iterator.hasNext();) {
			iterator.next().setId(craftsman.getId()).setCode(craftsman.getCode()).setCreatedBy(operator)
					.setUpdatedBy(operator);
		}
		if (dao.insert(NAMESPACE, craftsmanJobBackList) < craftsmanJobBackList.size())
			throw new DataException(DataException.Type.FAIL, errorMsg);
	}
}
